﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Billing
{
    public class DependAssembly
    {
        DependencyTypes mDependencyType;
        string mCodebase;
        long mSize;
        AssemblyIdentity mAssemblyIdentity;

        public DependencyTypes DependencyType
        {
            get { return mDependencyType; }
            set { mDependencyType = value; }
        }
        public string Codebase
        {
            get { return mCodebase; }
            set { mCodebase = value; }
        }
        public long Size
        {
            get { return mSize; }
            set { mSize = value; }
        }
        public AssemblyIdentity AssemblyIdentity
        {
            get { return mAssemblyIdentity; }
            set { mAssemblyIdentity = value; }
        }

        public DependAssembly(XmlNode elm, XmlNamespaceManager nsmgr)
        {
            if (elm.Attributes["codebase"] != null)
                Codebase = elm.Attributes["codebase"].Value;
            if (elm.Attributes["dependencyType"] != null)
                DependencyType = (DependencyTypes)Enum.Parse(typeof(DependencyTypes), elm.Attributes["dependencyType"].Value, true);
            if (elm.Attributes["size"] != null)
                Size = long.Parse(elm.Attributes["size"].Value);

            // Load assembly identity
            XmlNode aiNode =
                elm.SelectSingleNode("asmv2:assemblyIdentity", nsmgr);
            if (aiNode != null)
            {
                AssemblyIdentity = new AssemblyIdentity(aiNode, nsmgr);
            }
        }
    }

    public class AssemblyFile
    {
        string mName;
        int mSize;
        string mGroup;

        public string Name
        {
            get { return mName; }
            set { mName = value; }
        }
        public int Size
        {
            get { return mSize; }
            set { mSize = value; }
        }
        public string Group
        {
            get { return mGroup; }
            set { mGroup = value; }
        }

        public AssemblyFile(XmlNode elm, XmlNamespaceManager nsmgr)
        {
            if (elm.Attributes["name"] != null)
                Name = elm.Attributes["name"].Value;
            if (elm.Attributes["size"] != null)
                Size = int.Parse(elm.Attributes["size"].Value);
            if (elm.Attributes["group"] != null)
                Group = elm.Attributes["group"].Value;
        }
    }

    public class AssemblyIdentity
    {
        private string mName;
        private Version mVersion;
        private string mPublicKeyToken;
        private string mCulture;

        public string Name
        {
            get { return mName; }
            set { mName = value; }
        }
        public Version Version
        {
            get { return mVersion; }
            set { mVersion = value; }
        }
        public string PublicKeyToken
        {
            get { return mPublicKeyToken; }
            set { mPublicKeyToken = value; }
        }
        public string Culture
        {
            get { return mCulture; }
            set { mCulture = value; }
        }

        public AssemblyIdentity(XmlNode elm, XmlNamespaceManager nsmgr)
        {
            if (elm.Attributes["name"] != null)
                Name = elm.Attributes["name"].Value;
            if (elm.Attributes["version"] != null)
                Version = new Version(elm.Attributes["version"].Value);
            if (elm.Attributes["publicKeyToken"] != null)
                PublicKeyToken = elm.Attributes["publicKeyToken"].Value;
            if (elm.Attributes["language"] != null)
                Culture = elm.Attributes["language"].Value;
        }
    }

    public enum DependencyTypes
    {
        Install,
        Prerequisite
    }
}
